package cn.pconline.autoclub.service; 

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubLimit;
import cn.pconline.autoclub.util.DateUtils;
import org.gelivable.dao.GeliDao;
import org.springframework.beans.factory.annotation.Autowired;

/** 
 * @author 作者 guoqiang
 * @version 2014-3-25
 */
public class ClubLimitService {
	
	@Autowired
	private GeliDao geliDao;
	@Autowired
	private ClubService clubService;
	
	public int getClubTotalAdd(long clubId){
		Club club = clubService.getClub(clubId);
		int petrol = 0 ;
		if(club != null){
			petrol = (club.getVipNum()+1)*100;
		}
		return petrol;
	}
	

	/**
     * 查找某天的车友会限制信息,不会为空
     *
     * @param uid 用户id
     * @param ymd 日期,yyyyMMdd格式
     * @return
     */
    public ClubLimit getMemberLimit(long clubId, int ymd) {
        String sql = "SELECT * FROM ac_club_limit WHERE club_id = ? AND ymd = ?";
        ClubLimit limit = geliDao.findFirst(ClubLimit.class, sql, clubId, ymd);
        if (limit == null) {
        	limit = new ClubLimit();
        	limit.setClubId(clubId);
            limit.setYmd(ymd);
            geliDao.create(limit);
        }
        return limit;
    }

    /**
     * 查找当天的车友会限制信息,不会为空
     *
     * @param uid 用户id
     * @return
     */
    public ClubLimit getMemberLimit(long clubId) {
        return getMemberLimit(clubId, DateUtils.formatData2YMD());
    }

    /**
     * 更新车友会限制信息
     * @param uid 用户id
     * @param petrol 加分值
     * @return
     */
    public int update(long clubId, int petrol) {
        String sql = "SELECT * FROM ac_club_limit WHERE club_id = ? AND ymd = ?";
        int time = DateUtils.formatData2YMD();
        ClubLimit limit = geliDao.findFirst(ClubLimit.class, sql, clubId, time);
        if (limit == null) { // 新增
        	limit = new ClubLimit();
            limit.setClubId(clubId);
            limit.setYmd(time);
            limit.setPetrol(petrol);
            long limitId = geliDao.create(limit);
            return limitId > 0 ? 1 : 0;
        }
        
        // 更新
        limit.setPetrol(limit.getPetrol() + petrol);
        return geliDao.update(limit);
    }
}
